package com.ddpt.platform.common.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class BeanTransferUtils {

    private static final CopyOptions copyOptions = CopyOptions.create().setIgnoreNullValue(true);

    public static<I,O> List<O> transferCollection(Collection<I> input ,Class<O> clz){
        List<O> ret =new ArrayList<>(input.size());
        Iterator<I> iterator = input.iterator();
        while (iterator.hasNext()){
            O result =transferBean(iterator.next(),clz);
            ret.add(result);
        }
        return ret;
    }

    public static<I,O> O transferBean(I input ,Class<O> clz){
        O result =null;
        try {
            result =clz.newInstance();
            BeanUtil.copyProperties(input,result,copyOptions);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return result;
    }
}
